from datetime import datetime

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

from apps.tool import str2Hump


class Base(db.Model):
    __abstract__ = True
    createTime = db.Column(db.DateTime, name="create_time", default=datetime.now, comment="创建时间")
    updateTime = db.Column(db.DateTime, name="update_time", default=datetime.now, onupdate=datetime.now, comment="更新时间")
    isDeleted = db.Column(db.Integer, name="is_deleted", default=0, comment="逻辑删除:0=未删除,1=删除")

    def to_dict(self):
        """
        对象转字典
        :return:
        """
        if self is not None:
            return {str2Hump(c.name): getattr(self, str2Hump(c.name)) for c in self.__table__.columns}
        else:
            return {}

    def dict2obj(self, dict):
        """
        字典黑屏对象
        :param dict:
        :return:
        """
        self.__dict__.update(dict)
        return self
